<template>
	<view id="activity">
		<myTab v-model="index" :tabs="oa_product_work_type" @change="typeChange"></myTab>
		<me-tabs class="dataBar" :tabWidth="150" active-color="#FFF" v-model="productIndex" :tabs="productList" :nameKey="'cate'" @change="catChange"></me-tabs>
		<mescroll-uni ref="mescrollRef" @init="mescrollInit" top="190" bottom="0" @down="downCallback" :up="upOption" :down="downOption" @up="upCallback">
			<view  class="materialList"  v-for="(item,index) in list" :key="index">
				<view class="head">
					<image  :src="item.listImg" alt="" v-if='item.listImg' />
					<image  src="../../static/logo.png" v-else  alt="" />
					<view class="msg">
						<view class="title">{{item.title}}</view>
						<view class="time">{{item.createTime}}</view>
					</view>
				</view>
				<view class="content" v-html='item.zcontent'></view>
				<view class="shareImg" v-if='item.cover' >
					<image @load="imgLoad($event,item)" :class="item.imgWidth>item.imgHeight?'imgw':''" mode="widthFix"  :src="item.cover" alt=""  @click='previewImg(item.cover)' />
				</view>
				<view class="operation">
					<text @click='shareImg(item.cover,item.content)' >分享素材</text>
				</view>
			</view>
		</mescroll-uni>
		<uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
			<view class="shareBox">
				<view class="shareDiv">
					<image src="../../static/index/wx.png" @click="wxShare(1)"/>
					<view>微信</view>
				</view>
				<view class="shareDiv">
					<image src="../../static/index/wx-circle.png" @click="wxShare(2)"/>
					<view>朋友圈</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	import myTab from "@/components/myTab/myTab"
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	export default {
		mixins: [MescrollMixin],
		components: {
			myTab
		},
		data() {
			return {
				index: 0,
				type: 1,
				oa_product_work_type:[],
				productIndex:0,
				productList:[],
				productID:'',
				list:[],
				shareImage:'',
				content:'',
				upOption:{
					auto:false
				},
				downOption:{
					auto:false
				}
			}
		},
		mounted() {
			this.getProductList(2)
			this.getDictData()
		},
		methods: {
			typeChange(e) {
				this.index = e.i
				this.type = e.id
				this.getProductList()
			},
			reload() {
				this.list = []
				this.mescroll.resetUpScroll()
			},
			getDictData() {
				this.httpClient.post('system/manageApp/index/getDictData', {
					dictType: 'oa_product_work_type'
				}).then(res => {
					let statusList = []
					res.data.forEach((v, i) => {
						statusList.push({
							id: v.dictValue,
							name: v.dictLabel
						})
					})
					this.oa_product_work_type = statusList
				})
			},
			imgLoad(e,item){
				item.imgWidth=e.detail.width;
				item.imgHeight=e.detail.height;
				this.$forceUpdate()
			},
			//获取素材分类
			getProductList(i){
				this.httpClient.post('agents/app/ky/getKyCatList', {productWorkType:this.type}).then(res=>{
					if(res.code==200){
						this.productList =res.data
						this.productIndex =0
						this.productID=res.data[0].id
						this.list = []
						this.mescroll.resetUpScroll()
					}
				})
			},
			//素材分类切换
			catChange(e){
				this.productID=this.productList[e].id
				this.list = []
				this.mescroll.resetUpScroll()
			},
			//list
			upCallback(page) {
				console.info(page)
				this.httpClient.post('agents/app/ky/index', {
					pageNum:page.num,
					pageSize:page.size,
					productWorkType:this.type,
					cateId:this.productID
				}).then(res => {
					this.mescroll.endByPage(res.rows.length, res.total)
					if (page.num == 1) this.list = [];
					this.list = this.list.concat(res.rows);
				}).catch(() => {
					this.mescroll.endErr();
				})
			},
			//预览图片
			previewImg(imgUrl) {
				uni.previewImage({
					urls: [imgUrl ]
				})
			},
			//分享按钮
			shareImg(img,content){
				if(img){
					this.shareImage=img
					this.content=content
					this.$refs.share.open()
				}else{
					uni.showToast({
						icon: 'none',
						title: '素材中没有图片，无法分享'
					});
				}
			},
			//分享
			wxShare(i){
				let shareType=''
				let that=this
				if(i==1){
					shareType='WXSceneSession'//分享到微信
				}else if(i==2){
					shareType='WXSceneTimeline'//分享到朋友圈
				}else{
					uni.showToast({
						icon: 'none',
						title: '系统错误'
					})
					return
				}
				uni.setClipboardData({
					data: that.content,
					showToast:false,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: '文字已复制，图片加载中...'
						})
					}
				});
				uni.share({
					provider: "weixin",
					scene: shareType,
					type: 2,
					imageUrl: that.shareImage,
					success: function (res) {
						uni.showToast({
							icon: 'none',
							title: '分享成功'
						})
					},
					fail: function (err) {
						console.log("fail:" + JSON.stringify(err));
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	#activity{
		.materialList{
			padding: 1rem;
			background: #fff;
			margin-bottom: 15rpx;
			.head{
				height: 3rem;
				display: flex;
				align-items: center;
				image{
					width: 3rem;
					height: 3rem;
					margin-right: 0.6rem;
				}
				.msg{
					font-size: 0.9rem;
				}
				.time{
					font-size: 0.8rem;
					color: #888;
				}
			}
			.content{
				margin:10rpx 0;
			}
			.shareImg{
				width:100%;
				image{
					width: 40%;
					margin-right: 3%;
				}
				.imgw{
					width: 86% !important;
				}
			}
			.operation{
				margin-top:15rpx;
				text-align: right;
				text{
					border: 1px solid #eee;
					display: inline-block;
					padding: 0.3rem 1rem;
					font-size: 0.8rem;
					color: #fff;
					background: #3aacff;
					border-radius: 20px;
					margin-right: 0.4rem;
				}
			}
		}
		.shareBox{
			padding:30rpx;
			.shareDiv{
				width:100rpx;height:150rpx;display:inline-block;margin:20rpx;font-size:26rpx;text-align: center;
				image{
					width:100rpx;height:100rpx;
				}
			}
		}
	}
</style>
